import axios from '@/utils/axios'

const BASE_URL = '/v1/customer/customerrelation'

// 客户查询参数类型
export interface CustomerQueryParams {
    name_LIKE?: string
    phone_LIKE?: string
    email_LIKE?: string
    gender_EQ?: string
    customerType_EQ?: string
    status_EQ?: string
}

// 学生查询参数类型
export interface StudentQueryParams {
    name_LIKE?: string
    grade_EQ?: string
    gender_EQ?: string
    status_EQ?: string
    school_LIKE?: string
    className_EQ?: string
    educationType_EQ?: string
}

// 客户关系查询参数类型
export interface CustomerStudentRelationQueryParamsType {
    // 基础查询条件
    customerId_EQ?: string
    studentId_EQ?: string
    relationType_EQ?: string
    isPrimary_EQ?: string

    // 时间范围查询
    createTime_GE?: string
    createTime_LE?: string
    createBy_EQ?: string

    // 嵌套查询参数
    customerQP?: CustomerQueryParams
    studentQP?: StudentQueryParams

    // 分页参数
    pageNo?: number
    pageSize?: number
}

// 客户关系模型类型
export interface CustomerStudentRelationModel {
    id?: string
    customerId?: string
    studentId?: string
    relationType?: string
    isPrimary?: string

    // 客户信息字段
    customerName?: string
    customerGender?: string
    customerPhone?: string
    customerEmail?: string
    customerIdCard?: string
    customerAddress?: string
    customerType?: string
    customerStatus?: string
    customerSourceChannel?: string
    customerTags?: string
    customerRemark?: string

    // 学生信息字段
    studentName?: string
    studentGender?: string
    studentBirthDate?: string
    studentSchool?: string
    studentGrade?: string
    studentClassName?: string
    studentHealthInfo?: string
    studentSpecialNeeds?: string
    studentEducationType?: string
    studentStatus?: string

    // 系统字段
    createTime?: string
    createBy?: string
    updateTime?: string
    updateBy?: string
}

/**
 * 获取客户关系详细信息分页列表（包含客户和学生信息）
 * @param params 查询参数
 */
export const getListWithDetails = (params: CustomerStudentRelationQueryParamsType): Promise<any> => {
    // 构建查询参数，使用下划线分隔的字段名（MyBatis-Plus风格）
    const queryParams: Record<string, any> = {};

    // 基础查询条件
    if (params.customerId_EQ) queryParams.customerId_EQ = params.customerId_EQ;
    if (params.studentId_EQ) queryParams.studentId_EQ = params.studentId_EQ;
    if (params.relationType_EQ) queryParams.relationType_EQ = params.relationType_EQ;
    if (params.isPrimary_EQ) queryParams.isPrimary_EQ = params.isPrimary_EQ;

    // 时间范围查询
    if (params.createTime_GE) queryParams.createTime_GE = params.createTime_GE;
    if (params.createTime_LE) queryParams.createTime_LE = params.createTime_LE;
    if (params.createBy_EQ) queryParams.createBy_EQ = params.createBy_EQ;

    // 客户查询条件 - 使用点号分隔的嵌套参数
    if (params.customerQP) {
        if (params.customerQP.name_LIKE) queryParams['customerQP.name_LIKE'] = params.customerQP.name_LIKE;
        if (params.customerQP.phone_LIKE) queryParams['customerQP.phone_LIKE'] = params.customerQP.phone_LIKE;
        if (params.customerQP.email_LIKE) queryParams['customerQP.email_LIKE'] = params.customerQP.email_LIKE;
        if (params.customerQP.gender_EQ) queryParams['customerQP.gender_EQ'] = params.customerQP.gender_EQ;
        if (params.customerQP.customerType_EQ) queryParams['customerQP.customerType_EQ'] = params.customerQP.customerType_EQ;
        if (params.customerQP.status_EQ) queryParams['customerQP.status_EQ'] = params.customerQP.status_EQ;
    }

    // 学生查询条件 - 使用点号分隔的嵌套参数
    if (params.studentQP) {
        if (params.studentQP.name_LIKE) queryParams['studentQP.name_LIKE'] = params.studentQP.name_LIKE;
        if (params.studentQP.grade_EQ) queryParams['studentQP.grade_EQ'] = params.studentQP.grade_EQ;
        if (params.studentQP.gender_EQ) queryParams['studentQP.gender_EQ'] = params.studentQP.gender_EQ;
        if (params.studentQP.status_EQ) queryParams['studentQP.status_EQ'] = params.studentQP.status_EQ;
        if (params.studentQP.school_LIKE) queryParams['studentQP.school_LIKE'] = params.studentQP.school_LIKE;
        if (params.studentQP.className_EQ) queryParams['studentQP.className_EQ'] = params.studentQP.className_EQ;
        if (params.studentQP.educationType_EQ) queryParams['studentQP.educationType_EQ'] = params.studentQP.educationType_EQ;
    }

    // 添加分页参数
    if (params.pageNo) queryParams.pageNo = params.pageNo;
    if (params.pageSize) queryParams.pageSize = params.pageSize;

    return axios.get(`${BASE_URL}/findPageWithDetails`, { params: queryParams });
};

/**
 * 根据ID获取客户关系详细信息
 * @param id 关系ID
 */
export const getWithDetails = (id: string): Promise<any> => {
    return axios.get(`${BASE_URL}/getWithDetails`, { params: { id } });
};

/**
 * 新增客户关系
 * @param data 客户关系数据
 */
export const create = (data: CustomerStudentRelationModel): Promise<any> => {
    return axios.post(`${BASE_URL}/insert`, data);
};

/**
 * 更新客户关系
 * @param data 客户关系数据
 */
export const update = (data: CustomerStudentRelationModel): Promise<any> => {
    return axios.post(`${BASE_URL}/update`, data);
};

/**
 * 删除客户关系
 * @param id 关系ID
 */
export const deleteRelation = (id: string): Promise<any> => {
    return axios.post(`${BASE_URL}/del`, null, { params: { id } });
};

/**
 * 批量删除客户关系
 * @param ids ID数组字符串，逗号分隔
 */
export const batchDelete = (ids: string): Promise<any> => {
    return axios.post(`${BASE_URL}/delAll`, null, { params: { ids } });
}; 